import '../bean/requesterror_bean.dart';
import '../bean/selectarealist_bean.dart';
import '../bean/userinfo_bean.dart';
import '../utils/api/http.dart';
import '../utils/tools/errorhandle_tool.dart';
import '../utils/tools/localstorage_tool.dart';

class BaseModel {
  /// ### 获取app版本号
  reqAppEdition(context) async {
    try {
      Map res =
          await HttpManager.getInstance().get("appEditionQuery/getAppEdition");
      return res["data"];
    } on RequestErrorBean catch (e) {
      e.msg = "检查更新失败";
      errorHandle(context, e);
    }
  }

  /// ### 登录
  /// #### [par] Map|必填|用户名密码
  static Future reqlogin(par) async {
    var res = await HttpManager.getInstance().post('login', params: par);
    return res["data"];
  }

  /// ### 获取基础数据（区域）
  static Future reqQueryAllJson() async {
    String token = await LocalStorage.getStringItem("token");
    var res = await HttpManager.getInstance()
        .post('/user/selectAreaByUser?token=${token}');
    return res["data"];
  }

  /// ### 获取台区列表
  /// #### [par] Map|必填|台区请求参数，详细请参考api接口文档
  static Future reqAreaMetaList(par) async {
    String token = await LocalStorage.getStringItem("token");
    var res = await HttpManager.getInstance()
        .post('equipment/queryByArea?token=${token}', params: par);
    return SelectAreaListBean(res["data"]);
  }

  /// ### 获取当前用户信息
  static Future reqUserInfo() async {
    String token = await LocalStorage.getStringItem("token");
    var res =
        await HttpManager.getInstance().post('user/getById?token=${token}');
    //return UserInfoBean.fromJson(res["data"]);
    return UserInfoBean.getInstance().fromJson(res["data"]);
  }

  /// ### 退出
  static Future reqLogout() async {
    String token = await LocalStorage.getStringItem("token");
    var res = await HttpManager.getInstance().get('logout?token=${token}');
    return res;
  }

  /// ### 修改密码
  /// #### [par] Map|必填|新密码
  static Future reqChangePwd(Map par) async {
    String token = await LocalStorage.getStringItem("token");
    var res = await HttpManager.getInstance()
        .post('user/editPwd?token=${token}', params: par);
    return res;
  }
}
